package com.gz.mylife.app;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar.LayoutParams;
import android.util.Log;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.gz.mylife.App;
import com.gz.mylife.Define;
import com.gz.mylife.R;
import com.zl.smartmall.library.util.Tools;

public class BaseActivity extends ActionBarActivity {
	private String TAG = BaseActivity.class.getSimpleName();
	
	protected ActionBar mActionBar;
	private ExitReceiver mExitReceiver;
	protected TextView mActionbarTitle;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		mActionBar = getSupportActionBar();
		if(getClass() != Plaza.class && getClass() != Login.class) {
			mActionBar.setHomeButtonEnabled(true);
			mActionBar.setDisplayHomeAsUpEnabled(true);
			mActionBar.setDisplayShowHomeEnabled(true);
//			mActionBar.setDisplayShowHomeEnabled(false);
//			mActionBar.setDisplayShowTitleEnabled(true);
			mActionBar.setDisplayUseLogoEnabled(true);
		
		}else { 
	        mActionBar.setHomeButtonEnabled(false);
			mActionBar.setDisplayHomeAsUpEnabled(false);
		}
		mActionBar.setDisplayShowTitleEnabled(false);
		mActionBar.setIcon(null);
		mActionBar.setLogo(R.color.transparent);
		View actionView = getLayoutInflater().inflate(R.layout.action_bar, null);
		mActionbarTitle = (TextView) actionView.findViewById(R.id.tv_actionbar_title);
		mActionBar.setCustomView(actionView, new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.MATCH_PARENT, Gravity.CENTER));
		mActionBar.setDisplayShowCustomEnabled(true);
		
		mExitReceiver = new ExitReceiver();
		IntentFilter intentFilter = new IntentFilter(Define.ACTION_EXIT_WHOLE_APP);
		registerReceiver(mExitReceiver, intentFilter);
	}
	
	
	public void showlog(String msg){
		if(App.showlog){
			Log.i(TAG, msg);
		}
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unregisterReceiver(mExitReceiver);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();		
			return true;

		default:
			return super.onOptionsItemSelected(item);
		}
		
	}
	
	/**
	 * 设置actionbar标题
	 * @param title 标题字符串
	 * @param hasMenu 是否有actionbar菜单
	 */
	public void setActionbarTitle(String title, boolean hasMenu) {
		if(!hasMenu) {
			mActionbarTitle.setPadding(Tools.dip2px(getApplicationContext(), 3), 0, 0, 0);
		}
		mActionbarTitle.setText(title);
	}

	class ExitReceiver extends BroadcastReceiver {
		
		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			finish();
		}
	};

}
